home *** CD-ROM | disk | FTP | other *** search
/ The Original Shareware 1.1 / The Original Shareware (WeMake CDs)(Volume 1.1)(CDs, Inc)(1993).iso / 6 / cljune86.zip / STOOLS2.LTG < prev    next >
Text File  |  1986-03-31  |  7KB  |  193 lines

  1.                             Listing 2
  2.  
  3. #include "stdio.h"
  4. #include "dos.h"
  5.  
  6. #define BUF_SIZE 512
  7. char    buf[2 * BUF_SIZE],    /*------- Input text file buffer -------*/
  8.             s_buf[BUF_SIZE];
  9.  
  10. /******************************************************************************/
  11. /*----------------------------- The BUMP utility -----------------------------*/
  12. /******************************************************************************/
  13. /*                                                                            */
  14. /*  Name: bump                           Version: 1.3                         */
  15. /*                                                                            */è/*  Last Edit: 03/21/86                  Compiler: Lattice C, v2.15d          */
  16. /*                                                                            */
  17. /*  Purpose:  Take a passed name with embedded number and add one to the      */
  18. /*            number.  Search specified files for references to original      */
  19. /*            name and change to references to new name, (with bumped number).*/
  20. /*                                                                            */
  21. /******************************************************************************/
  22. /*------------------ Copyright 1985 & 1986, by Gary Elfring ------------------*/
  23. main(argc,argv)
  24.     int        argc;
  25.     char    *argv[];
  26.     {
  27.     extern    FILE    *fopen();
  28.     extern    char    *stpchr();
  29.     extern    long    ftell();
  30.     extern    char    *calloc();
  31.  
  32.     FILE    *in_file;
  33.  
  34.     char *t,*s,*data,*old;
  35.     char    file_old[80],
  36.                 file_new[80];
  37.     char    *p,
  38.                 ext[4],
  39.                 b_name[9],        /*------ Part of file name before embedded number ------*/
  40.                 e_name[9],        /*------- Part of file name after embeded number -------*/
  41.                 num[9],                /*--------- ASCII number embeded in file name ----------*/
  42.                 control[10];
  43.  
  44.     int        i, j,                    /*------------------- Work counters --------------------*/
  45.                 length,
  46.                 cnt,                    /*------------ Count of number char in name ------------*/
  47.                 position,            /*--------- Flag to tell where in name we are ----------*/
  48.                 bytes_read,
  49.                 found;
  50.  
  51.     unsigned    file_point;
  52.  
  53.     long    pos;
  54.  
  55. /*----- Give them some help if they don't know how to invoke the program -----*/
  56.     if (argc < 3)
  57.         {
  58.         printf("BUMP searches a file or series of files for a specific file name.\n");
  59.         printf("This name must end in a number.  Anytime the file name is found the\n");
  60.         printf("number portion will be increased by one.\n\n");
  61.         printf("USE --> bump file1 file2 file3 ... fileN nameXX.ext{cr}\n");
  62.         printf("        where file1-N are files to search\n");
  63.         printf("        and nameXX.ext is any file name with the name part ending\n");
  64.         printf("        in a 1 to 4 digit number.\n");
  65.         exit(1);
  66.         }
  67.  
  68. /*-------------------------- Get file name to bump ---------------------------*/
  69.     strcpy(file_old,argv[argc - 1]);è
  70. /*--------------------------- Rip name into parts ----------------------------*/
  71. /*----------------------- Fool with extension if found -----------------------*/
  72.     if ((p = stpchr(file_old,'.')) != NULL)
  73.         {
  74.         *p++ = 0;
  75.         strcpy(ext,p);
  76.         }
  77.     else
  78.         ext[0] = 0;
  79.  
  80. /*------------------------ Need length of whats left -------------------------*/
  81.     length = strlen(file_old);
  82.  
  83. /******************************************************************************/
  84. /*-------------- Now rip whats left into numer and other parts ---------------*/
  85. /******************************************************************************/
  86.     b_name[0] = e_name[0] = num[0] = cnt = 0;
  87.     position = 0;
  88.     for (i = 0, t = &file_old[0], p = &b_name[0]; i < length; ++i)
  89.         {
  90.         /*-------------- If we havent found any numbers in name yet --------------*/
  91.         if (position == 0)
  92.             {
  93.             /*------------------- Is this the first digit found --------------------*/
  94.             if (isdigit(*t))
  95.                 {
  96.                 *p = 0;                /*-------------- End the beginning string --------------*/
  97.                 ++cnt;
  98.                 ++position;
  99.                 p = &num[0];    /*------ Shift the pointer to the number storage -------*/
  100.                 }
  101.             }
  102.         /*------------- Else if we already found a digit in the past -------------*/
  103.         else if (position == 1)
  104.             {
  105.             /*--------------------- See if still doing digits ----------------------*/
  106.             if (isdigit(*t))
  107.                 ++cnt;
  108.             else        /*--------- If we just stopped finding digits, switch ----------*/
  109.                 {
  110.                 *p = 0;
  111.                 ++position;
  112.                 p = &e_name[0];  /*----------- Move poiter to end storage ------------*/
  113.                 }
  114.             }
  115.         *p++ = *t++;  /*------------ ALWAYS save character somewhere -------------*/
  116.         }
  117.     *p = 0;
  118.  
  119.     strcpy(file_old,argv[argc - 1]);
  120.     /*---- We need to build a format control string with # of digits in it -----*/
  121.     sprintf(control,"%%s%%0%dd%%s",cnt);
  122.     /*-------------------- Now build the "bumped" file name --------------------*/
  123.     sprintf(file_new,control,b_name,atoi(num) + 1,e_name);è    if (ext[0] != 0)
  124.         {
  125.         strcat(file_new,".");
  126.         strcat(file_new,ext);
  127.         }
  128.     printf("Changing all reference to %s to %s in files:\n",file_old,file_new);
  129.     for (i = 1; i < (argc - 1); ++i)
  130.         printf("%s ",argv[i]);
  131.     printf("\n");
  132.  
  133. /*---------------------- While there are files to bump -----------------------*/
  134.     for (i = 1; i < (argc - 1); ++i)
  135.         {
  136.         found = 0;
  137.         /*----------------- Make sure the file to search exists ------------------*/
  138.         if ((in_file = fopen(argv[i],"rb+")) == NULL)
  139.             {
  140.             printf("Error: can't open file %s\n",argv[i]);
  141.             exit(1);
  142.             }
  143.  
  144.         /*------------------ File size must be less than 64000 -------------------*/
  145.         fseek(in_file,0L,2);
  146.         if ((pos = ftell(in_file)) > 64000L)
  147.             {
  148.             fclose(in_file);
  149.             printf("Sorry, can not work with files larger than 64000 bytes\n");
  150.             exit(1);
  151.             }
  152.         fseek(in_file,0L,0);
  153.  
  154.         /*----------------- Allocate the storage for search area -----------------*/
  155.         if ((data = calloc((unsigned)pos,1)) == NULL)
  156.             {
  157.             fclose(in_file);
  158.             printf("Sorry, can not allocate enough memory\n");
  159.             exit(1);
  160.             }
  161.  
  162.         /*------------------------- Read in entire file --------------------------*/
  163.         bytes_read = fread(data,1,(unsigned)pos,in_file);
  164.  
  165.         /*--------------- Look through entire file for all matches ---------------*/
  166.         for (file_point = 0; file_point < bytes_read - length; ++file_point)
  167.             {
  168.             t = old = &data[file_point];
  169.             s = &file_old[0];
  170.           for ( ; *t == *s; ++t)
  171.                 {
  172.                 if(!*++s)
  173.                     {
  174.                     /*------------------------- Found a match! -------------------------*/
  175.                     found = !0;
  176.                     for (j = 0; j < length; ++j)
  177.                         *old++ = file_new[j];      /*------- Replace with new name -------*/è                    }
  178.                 }  /*---------------------- End for char = char ----------------------*/
  179.             }  /*------------------------- End the search --------------------------*/
  180.  
  181.         if (!found)
  182.             printf("Could not find %s in file %s\n",file_old,argv[i]);
  183.         else
  184.             {
  185.             fseek(in_file,0L,0);
  186.             fwrite(data,bytes_read,1,in_file);
  187.             }
  188.         fclose(in_file);
  189.         free(data);
  190.         }  /*---------------------- End while text to read -----------------------*/
  191.     }
  192.  
  193.